package cn.datawin.cmppprovider.task;

import cn.datawin.cmppprovider.util.RandUtil;
import cn.hutool.core.date.DateUtil;
import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.common.SequenceNumber;
import com.drondea.sms.common.util.MsgId;
import com.drondea.sms.common.util.SmgpMsgId;
import com.drondea.sms.message.cmpp.CmppDeliverRequestMessage;
import com.drondea.sms.message.cmpp.CmppReportRequestMessage;
import com.drondea.sms.message.smgp30.msg.SmgpDeliverRequestMessage;
import com.drondea.sms.message.smgp30.msg.SmgpReportMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayTask implements Delayed {

    private static final Logger logger = LoggerFactory.getLogger(DelayTask.class);
    private ChannelSession channelSession;
    private MsgId msgId;
    private SmgpMsgId smgpMsgId;
    private String destTerminalId;
    private long delayTime;
    private int type;
    private String destId;

    public DelayTask(ChannelSession channelSession, MsgId msgId, String destTerminalId, long delayTime, int type, String destId) {
        this.channelSession = channelSession;
        this.msgId = msgId;
        this.destTerminalId = destTerminalId;
        this.destId = destId;
        // 延迟时间加当前时间
        this.delayTime = System.currentTimeMillis() + delayTime;
        this.type = type;
    }

    public DelayTask(ChannelSession channelSession, SmgpMsgId smgpMsgId, String destTerminalId, long delayTime, int type) {
        this.channelSession = channelSession;
        this.smgpMsgId = smgpMsgId;
        this.destTerminalId = destTerminalId;
        // 延迟时间加当前时间
        this.delayTime = System.currentTimeMillis() + delayTime;
        this.type = type;
    }

    // 获取任务剩余时间
    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(delayTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return Long.compare(delayTime, ((DelayTask) o).delayTime);
    }

    void deal() {

        if (type == 1) {

            int randNum = RandUtil.getForDelivery();
            //未知处理直接返回
            if (randNum == 2) {
                logger.debug("type=1,destTerminalId={},msgid={},delivery=2", destTerminalId, msgId);
                return;
            }

            //        long start = System.currentTimeMillis();

            SequenceNumber sequenceNumber = channelSession.getSequenceNumber();
            //模拟发送状态回执
            CmppDeliverRequestMessage deliverRequestMessage = new CmppDeliverRequestMessage();

            deliverRequestMessage.getHeader().setSequenceId(sequenceNumber.next());
            deliverRequestMessage.setRegisteredDelivery((short) 1);
            deliverRequestMessage.setDestId(destId);
            //状态信息
            CmppReportRequestMessage report = new CmppReportRequestMessage();
            report.setMsgId(msgId);

            if (randNum == 0) {
                report.setStat("DELIVRD");
            } else if (randNum == 1) {
                report.setStat(RandUtil.getReportStat());
            }
//            report.setStat("GB:0001");
//            report.setStat("DELIVRD");
//        logger.info("DELIVRD-------------------");

            report.setDestterminalId(destTerminalId);
            //        Thread.sleep(3000);
            String strCurDate = DateUtil.format(new Date(), "yyMMddHHmm");
            report.setSubmitTime(strCurDate);
            report.setDoneTime(strCurDate);
            deliverRequestMessage.setReportRequestMessage(report);
            channelSession.sendMessage(deliverRequestMessage);

            logger.debug("type=1,destTerminalId={},msgid={},delivery={}", destTerminalId, msgId, randNum);

        } else if (type == 2) {

            int randNum = RandUtil.getForDelivery();
            //未知处理直接返回
            if (randNum == 2) {
                logger.debug("type=2,destTerminalId={},smgpMsgId={},delivery=2", destTerminalId, smgpMsgId);
                return;
            }

            SequenceNumber sequenceNumber = channelSession.getSequenceNumber();

            //模拟发送状态回执
            SmgpDeliverRequestMessage deliverRequestMessage = new SmgpDeliverRequestMessage();

            deliverRequestMessage.getHeader().setSequenceId(sequenceNumber.next());
            deliverRequestMessage.setIsReport(true);
            deliverRequestMessage.setSrcTermId(destTerminalId);
            //deliverRequestMessage.setDestTermId(destTerminalId);//接收短信的号码
            //状态信息
            SmgpReportMessage report = new SmgpReportMessage();
            report.setSmgpMsgId(smgpMsgId);
            if (randNum == 0) {
                report.setStat("DELIVRD");
            } else if (randNum == 1) {
                report.setStat(RandUtil.getReportStat());
            }

            String strCurDate = DateUtil.format(new Date(), "yyMMddHHmm");
            report.setSubTime(strCurDate);
            report.setDoneTime(strCurDate);
            deliverRequestMessage.setReport(report);
            deliverRequestMessage.setReserve("Reserve");
//        deliverRequestMessage.setLinkId("LinkId");
//        deliverRequestMessage.setTpPid((byte)8);
//        deliverRequestMessage.setTpUdhi((byte)6);

            channelSession.sendMessage(deliverRequestMessage);

            logger.debug("type=2,destTerminalId={},msgid={},delivery={}", destTerminalId, smgpMsgId, randNum);

        }


    }


}
